/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author HoangLiem
 */
public class ExecRootXML {

    /*
     * Hàm này lấy danh sách các tên CSDL trong root.xml
     */
    public static ArrayList<String> getDatabaseNamesFromRoot() {

        ArrayList<String> databaseNames = new ArrayList<String>();
        try {
            Document doc = ExecXML.getDocument("root.xml");
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("database");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    if (node.hasAttributes()) {
                        Element ele = (Element) node;
                        String str = ele.getAttribute("name");
                        databaseNames.add(str);
                    }
                }
            }
            if (databaseNames.isEmpty()) {
                databaseNames = null;
            }
        } catch (Exception e) {
            databaseNames = null;
            e.printStackTrace();
        }
        return databaseNames;
    }
    /*
     * Ham nay kiem tra 1 ten database co ton tai chua
     */

    public static boolean isExistedDatabase(String databaseName) {
        try {
            ArrayList<String> databaseNames = getDatabaseNamesFromRoot();
            return ExecXML.isExistedString(databaseNames, databaseName);
        } catch (Exception ex) {
            return true;
        }
    }
    /*
     * Hàm này thêm 1 tên CSDL trong root.xml
     */

    public static boolean addNewDatabaseNameToRoot(String databaseName) {
        boolean result = true;

        if (isExistedDatabase(databaseName)) {
            return result = false;
        }
        try {
            Document doc = ExecXML.getDocument("root.xml");
            Element database = doc.createElement("database");
            database.setAttribute("name", databaseName.toLowerCase());
            Element root = doc.getDocumentElement();
            root.appendChild(doc.createTextNode("\t"));
            root.appendChild(database);
            root.appendChild(doc.createTextNode("\n"));
            ExecXML.saveXmlFile(doc, "root.xml");
            //Tiep tuc tao ra 1 file xml tuong trưng cho database
            doc = ExecXML.createDocument("database");
            Node newRoot = doc.getDocumentElement();
            Element name = doc.createElement("name");
            name.setTextContent(databaseName.toLowerCase());
            newRoot.appendChild(doc.createTextNode("\n\t"));
            newRoot.appendChild(name);
            newRoot.appendChild(doc.createTextNode("\n"));
            ExecXML.saveXmlFile(doc, databaseName + ".xml");
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

    /*
     * Hàm này xóa 1 tên CSDL từ root, kiem tra ton tai truoc khi xoa
     */
    public static boolean deleteDatabaseNameFromRoot(String databaseName) {
        boolean result = true;
        if (!isExistedDatabase(databaseName)) {
            return result = false;
        }
        try {
            Document doc = ExecXML.getDocument("root.xml");
            Element root = doc.getDocumentElement();
            if (root.hasChildNodes()) {
                NodeList nodeList = root.getChildNodes();
                Element database = null;
                Node textNode = null;
                for (int i = 0; i < nodeList.getLength(); i++) {
                    Node node = nodeList.item(i);
                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        database = (Element) node;
                        if (database.getAttribute("name").equalsIgnoreCase(databaseName)) {
                            if (i > 0) {
                                textNode = nodeList.item(i - 1);
                                root.removeChild(database);
                                root.removeChild(textNode);
                            }
                            break;
                        }
                    }
                }
                ExecXML.saveXmlFile(doc, "root.xml");
            } else {
                result = false;
            }
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }
}
